<?php
include "../templates/db/db.php";

    $select_m=mysql_query('SELECT * FROM messages WHERE `id`='.$_GET['id'].' AND `to`="'.$_SESSION['username'].'"');
    $row_m=mysql_fetch_assoc($select_m);

	echo '
    <table border="0" style="border-collapse: collapse; margin-left: 20px; width: 650px; text-shadow: 1px 1px 1px #fff;">
      <tr style="background-color: #b7b6b6;;"><td align="right" style="width: 100px;"><p><b>Тема</b></p></td><td align="left" style="padding-left: 20px;">'.$row_m[topic].'</td></tr>
	  <tr><td colspan="2"><hr /></td></tr>
	  ';
	  
	  echo '
	  <tr style="background-color: #b7b6b6;"><td align="right"><p><b>От </b></p></td><td align="left" style="padding-left: 20px;">'.$row_m[from].'</td></tr>
	  ';
	  
	  echo '
	  <tr><td colspan="2"><hr /></td></tr>
	  <tr style="background-color: #b7b6b6;"><td align="right"><p><b>Изпратено</b></p></td><td align="left" style="padding-left: 20px;">'.date("d-m-Y", $row_m[date]).'</td></tr>
    </table>   
	';
	
	echo '<p style="margin-left: 25px;">'.$row_m['text'].'</p>';
	
	
	if($row_m['nickname']=="request"){
	
    ?>
       <form method="post" action="">
	      <input type="submit" name="submit_user" value="Одобри!" />
		  <input type="hidden" name="submit_user_hidden" value="1" />
		  <input type="hidden" name="user_hidden" value="<?php echo $row_m['from']; ?>" />
	   </form>
	<?php
	if($_POST['submit_user']==true){
	   if($_POST['submit_user_hidden']==1){
	      $upd = mysql_query('UPDATE users SET type=2, teacher_to="'.$row_m['topic'].'" WHERE username = "'.$_POST['user_hidden'].'"');
		  $upd_t = mysql_query('UPDATE tutorials SET teacher = "'.$row_m['from'].'" WHERE name = "'.$row_m['topic'].'"');
		  
		  $insert_message = mysql_query('INSERT INTO messages (`from`, `to`, `topic`, `text`, `date`, `nickname`) 
		 VALUES("admin", "'.$_POST['user_hidden'].'", "Кандидатстване за модератор", "Поздравления, вече си учител!",
		 '.time().', "applied")');
		 
		 $delete_message = mysql_query('DELETE FROM messages WHERE `from`="'.$_POST['user_hidden'].'" AND `nickname`="request"');
		 echo '<meta http-equiv="Refresh" content="0; URL=messages.php"/>';
	   }
	}
	} 
	
	$upd=mysql_query('UPDATE messages SET `read`=1 WHERE `id`='.$_GET['id'].'');
?>